package com.akupilih.app;

import java.util.List;

import org.afree.data.category.DefaultCategoryDataset;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import com.akupilih.app.chart.SocialMediaBarChartView;
import com.akupilih.app.entity.E_Chart;
import com.akupilih.app.helper.JSONHelper;
import com.akupilih.app.helper.VolleyHelper;
import com.akupilih.app.model.WebService;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;

public class SocialMediaBarChartFragment extends Fragment {
	private String URL;
	private static final String TAG = "Sosial Media Trend";
	private List<E_Chart> listchart;
	private FragmentActivity myContext;
	ProgressBar progressBar;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		progressBar = new ProgressBar(myContext, null, android.R.attr.progressBarStyleSmall);

		URL = WebService.POLITICAL_WAVE();
		container.addView(progressBar);
		SocialMediaBarChartView barchart = getData(getActivity(), URL);

		return barchart;
	}

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		myContext = (FragmentActivity) activity;

	}

	private SocialMediaBarChartView getData(final Context contex, String URL) {
		final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,
				URL, null, new Response.Listener<JSONObject>() {
					@Override
					public void onResponse(JSONObject obj) {

						try {
							JSONObject root = obj.getJSONObject("mediawave");
							JSONArray chart = root.getJSONArray("chart");
							JSONObject objChart = chart.getJSONObject(3);
							JSONArray data = objChart.getJSONArray("data");
							listchart = JSONHelper.getBarChart(data);

							for (int i = 0; i < listchart.size(); i++) {
								dataset.addValue(listchart.get(i).getSosmed_val1() , "Prabowo-Hatta", listchart.get(i).getSosmed());
								dataset.addValue(listchart.get(i).getSosmed_val2() , "Jokowi - Jusuf Kala", listchart.get(i).getSosmed());
								Log.d("data--->", listchart.get(i).getSosmed_val1() +"" );
								Log.d("data--->", listchart.get(i).getSosmed_val2() + "");
								Log.d("category-->",  listchart.get(i).getSosmed());
							
							}

						} catch (JSONException e) {
							ACRAPostSender.TRACE(TAG + " ERROR", "getData : "
									+ e.getMessage());
						}
					}
				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(VolleyError error) {
						// TODO Auto-generated method stub
						ACRAPostSender.TRACE(TAG
								+ ".getData(...).new ErrorListener() {...}",
								"onErrorResponse : " + error.toString());
						VolleyHelper.showVolleyError(contex, error);
					}
				});
		request.setRetryPolicy(new DefaultRetryPolicy(9000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
		BaseApplication.getInstance().addToRequestQueue(request, TAG);
		SocialMediaBarChartView trends = new SocialMediaBarChartView(contex, dataset, TAG);
		progressBar.setVisibility(View.GONE);
		return trends;

	}

}
